package br.com.felix.dbm.ui.components;

import javax.swing.JCheckBox;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import br.com.felix.dbm.controller.ColumnController;

public class NullableCheckBox extends JCheckBox {

	private static final long serialVersionUID = 2600300990074946024L;

	private ColumnController columnController;

	
	public NullableCheckBox(ColumnController columnController) {
		super();
		this.columnController = columnController;
		setText("NULL");
		setSelected(true);
		
		addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				boolean columnIsNullable = isSelected();
				ColumnController theColumnController = NullableCheckBox.this.columnController;
				if(columnIsNullable ^ (theColumnController.getModel().isNullAlowed())){
					theColumnController.allowNull(columnIsNullable);
				}
			}
		});
	}
	
	
}
